package com.miniboy.imageloader.util;

import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;

/**
 * 作者 ${郭鹏飞}.<br/>
 */
public class BitmapUtil {

    /**
     * 从资源中加载Bitmap
     *
     * @param resources Resources
     * @param resId     资源id
     * @param reqWidth  指定的宽
     * @param reqHeight 指定的高
     * @return Bitmap
     */
    public static Bitmap decodeSampleBitmap(Resources resources, int resId, int reqWidth, int reqHeight) {
        BitmapFactory.Options options = new BitmapFactory.Options();
        // 该属性设置为true的时候 不会加载图片到内存中 但会得图片的基本信息 如,宽高
        options.inJustDecodeBounds = true;
        options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
        options.inJustDecodeBounds = false;
        return BitmapFactory.decodeResource(resources, resId);
    }

    /**
     * 从流数据中得到Bitmap
     *
     * @param is        输入流
     * @param reqWidth  指定的宽
     * @param reqHeight 指定的高
     * @return {@link Bitmap}
     */
    public static Bitmap decodeSampleBitmap(InputStream is, int reqWidth, int reqHeight) {
        if (is == null) {
            throw new NullPointerException("InputStream 为空");
        }
        BitmapFactory.Options options = new BitmapFactory.Options();
        // 该属性设置为true的时候 不会加载图片到内存中 但会得图片的基本信息 如,宽高
        options.inJustDecodeBounds = true;
        options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
        options.inJustDecodeBounds = false;
        // 解析流数据
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        byte[] buffer = new byte[1024];
        int len;
        try {
            while ((len = is.read(buffer)) != -1) {
                baos.write(buffer, 0, len);
                baos.flush();
            }
            byte[] data = baos.toByteArray();
            if (data == null || data.length <= 0) {
                return null;
            }
            return BitmapFactory.decodeByteArray(data, 0, data.length, options);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                baos.close();
                baos = null;
            } catch (IOException e) {
                e.printStackTrace();
                baos = null;
            }
        }
        return null;
    }

    /**
     * 计算压缩比例
     *
     * @param options   {@link android.graphics.BitmapFactory.Options}
     * @param reqWidth  指定的宽
     * @param reqHeight 指定的高
     * @return 压缩值
     */
    public static int calculateInSampleSize(
            BitmapFactory.Options options, int reqWidth, int reqHeight) {
        // Raw height and width of image
        int inSampleSize = 1;
        if (reqWidth <= 0 || reqHeight <= 0) {
            return inSampleSize;
        }
        final int height = options.outHeight;
        final int width = options.outWidth;
        if (height > reqHeight || width > reqWidth) {
            final int halfHeight = height / 2;
            final int halfWidth = width / 2;
            // Calculate the largest inSampleSize value that is a power of 2 and keeps both
            // height and width larger than the requested height and width.
            while ((halfHeight / inSampleSize) > reqHeight
                    && (halfWidth / inSampleSize) > reqWidth) {
                inSampleSize *= 2;
            }
        }
        return inSampleSize;
    }


}
